#include "junix.h"

char *
base_name (char const *name)
{
	char const *base = name;
	char const *p;

	for (p = base; *p; p++){
		if ((*p)=='/')
		{
			/* Treat multiple adjacent slashes like a single slash.  */
			do p++;
			while ( (*p)=='/');

			/* If the file name ends in slash, use the trailing slash as
			 * 	     the basename if no non-slashes have been found.  */
			if (! *p)
			{
				if ((*base)=='/')
					base = p - 1;
				break;
			}

			/* *P is a non-slash preceded by a slash.  */
			base = p;
		}
	}

	return (char *) base;
}


int main(int argc, char **argv)
{
	printf("%s\n",basename("/hello////good") );
	printf("%s\n",basename("/hello/") );
    exit(0);
}

